do "E:\yungyu\lottery_baby\do\set_environment.do"

forv k = 1(1)5{ //discount rate
	use "$wdata\project_lottery_fertility_b3a6_first_5k.dta", clear

	keep if norm == -1
	keep houseid current pers_idn pers_gender age pers_earnings pers_income
	rename pers_gender gender
	rename pers_earnings earnings
	rename pers_income income

	gen lifetime_earnings_at55 = earnings
	gen lifetime_income_at55 = income
	gen lifetime_earnings_at60 = earnings
	gen lifetime_income_at60 = income
	gen lifetime_earnings_at65 = earnings
	gen lifetime_income_at65 = income
	gen lifetime_earnings_at70 = earnings
	gen lifetime_income_at70 = income

	gen age_pre1 = age
	gen earnings_pre1 = earnings
	gen income_pre1 = income

	forv i = 1(1)41{
		merge m:1 age gender using "$wdata/earnings_growthrate.dta", update replace
		drop if _m == 2
		drop _m

		replace age = age + 1
		
		replace earnings = earnings * (1+median_earnings_gr)
		replace income = income * (1+median_income_gr)
		
		forv j = 55(5)70{
			replace lifetime_earnings_at`j' = lifetime_earnings_at`j' + earnings / (1.0`k')^`i' if age <= `j'
			replace lifetime_income_at`j' = lifetime_income_at`j' + income / (1.0`k')^`i' if age <= `j'
		}
	}

	save "$wdata/lifetime_income_`k'.dta", replace
}

forv k = 1(1)5{ //discount rate
	use "$wdata/lifetime_income_`k'.dta", clear
	
	collapse (mean)lifetime*, by(current)
	
	reshape long lifetime_earnings_at lifetime_income_at, i(current) j(age)
	reshape long lifetime, i(current age) j(type) string
	
	replace type = "Lifetime Earning" if type == "_earnings_at"
	replace type = "Lifetime Income" if type == "_income_at"
	
	rename lifetime values
	gen discount_rate = `k'
	
	order current type age discount_rate values
	
	save "$wdata/lifetime_income_collapse_`k'.dta", replace

}

use "$wdata\project_lottery_fertility_b3a6_first_5k.dta", clear

gen tot_asset = pers_asset + spouse_asset
collapse (mean)pers_asset avg_asset tot_asset, by(current norm)

keep if norm == -1 | norm == 6
recode norm -1=0 6=1
reshape wide pers_asset avg_asset tot_asset, i(current) j(norm)

foreach x in pers_asset avg_asset tot_asset{
	gen `x' = `x'1-`x'0
	drop `x'1 `x'0
	rename `x' values`x'
}

reshape long values, i(current) j(type) string
replace type = "Baseline Trend in Personal Wealth" if type == "pers_asset"
replace type = "Baseline Trend in Total Wealth (w/ Couple)" if type == "tot_asset"
replace type = "Baseline Trend in Average Wealth (per Adult)" if type == "avg_asset"

forv k = 1(1)5{ //discount rate
	ap using "$wdata/lifetime_income_collapse_`k'.dta"
}

order current type age discount_rate values

drop if current == 1 & strpos(type,"Baseline") != 0
drop if current == 0 & strpos(type,"Lifetime") != 0

sort type age discount
replace discount = discount/100

gen elasticity = (0.063/0.321)/(5000/values)

drop current
tostring age, replace
tostring discount, replace format(%4.2f) force
tostring values, replace format(%9.0fc) force
tostring elasticity, replace format(%4.3f) force

export excel "$table/Tables.xlsx", sheet("FigD1") sheetreplace first(var)


import excel using "$table/Tables.xlsx", clear sheet(FigD1) first

drop if age == "."
destring age dis ela, replace force

twoway 	(sc elasticity discount if age == 65 & type == "Lifetime Earning", color(white) msize(tiny)) ///
		(line elasticity discount if age == 65 & type == "Lifetime Earning", lc(black) lw(medthick)) ///
		(line elasticity discount if age == 60 & type == "Lifetime Earning", lc(black) lp(longdash) lw(medthick)) ///
		(line elasticity discount if age == 55 & type == "Lifetime Earning", lc(black) lp(shortdash) lw(medthick)) ///
		(sc elasticity discount if age == 65 & type == "Lifetime Income", color(white) msize(tiny)) ///
		(line elasticity discount if age == 65 & type == "Lifetime Income", lc(gs10) lw(medthin)) ///
		(line elasticity discount if age == 60 & type == "Lifetime Income", lc(gs10) lp(longdash) lw(medthin)) ///
		(line elasticity discount if age == 55 & type == "Lifetime Income", lc(gs10) lp(shortdash) lw(medthin)), ///
		legend(col(4) symx(8) order(1 "Lifetime Earnings" 2 "Up to age 65" 3 "Up to age 60" 4 "Up to age 55" 5 "Lifetime Income" 6 "Up to age 65" 7 "Up to age 60" 8 "Up to age 55") size(small)) ///
		scheme(s1color) xtitle(Discount Rate) xlabel(0.01(0.01)0.05, format(%4.2f)) ylabel(0.15(0.05)0.4, angle(0) format(%4.2f)) ytitle(Elasticity)
		
graph export "$folder/figure/FigD1.eps", as(eps) replace
